package com.imay.live.manager;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by chan on 2017/4/2.
 */

public class EditTextManager {
    private static EditTextManager instance;

    public synchronized static EditTextManager getInstance() {
        if (instance == null) {
            instance = new EditTextManager();
        }
        return instance;
    }

    /**
     * 修改底框颜色
     *
     * @param view
     * @param preResDra
     * @param curResDra
     * @param editTexts
     */
    public void changeBgColor(final View view, final int preResDra, final int curResDra, final EditText... editTexts) {
        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                for (EditText editText : editTexts) {
                    Editable str = editText.getText();
                    String tag = (String) editText.getTag();
                    if (tag != null && tag.equals("psd")) {
                        if (TextUtils.isEmpty(str)) {
                            view.setBackground(view.getContext().getResources().getDrawable(preResDra));
                            view.setClickable(false);
                            return;
                        } else if (str.length() < 6) {
                            view.setBackground(view.getContext().getResources().getDrawable(preResDra));
                            view.setClickable(false);
                            return;
                        }
                    } else {
                        if (TextUtils.isEmpty(str)) {
                            view.setBackground(view.getContext().getResources().getDrawable(preResDra));
                            view.setClickable(false);
                            return;
                        }
                    }
                }
                view.setClickable(true);
                view.setBackground(view.getContext().getResources().getDrawable(curResDra));
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }
        };

        for (EditText editText : editTexts) {
            editText.addTextChangedListener(textWatcher);
        }
    }

    /**
     * 清楚数据
     *
     * @param imageView
     * @param editTexts
     */
    public void setEditClear(final ImageView imageView, final EditText... editTexts) {
        imageView.setVisibility(View.GONE);
        int size = editTexts.length;
        if (size > 0) {
            editTexts[0].setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        imageView.setVisibility(View.VISIBLE);
                    } else {
                        imageView.setVisibility(View.GONE);
                    }
                }
            });
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (EditText editText : editTexts) {
                        editText.setText("");
                    }
                }
            });
        }

    }
}
